package jksys

import (
	l4g "github.com/alecthomas/log4go"
	"jkgo/jk/jklog"
	cbase "findService/ss/servicebase"
)

func NewSysClientIoT(addr string, port int) (*SysClient, error) {
	l4g.Debug("New sys client of IoT [%s:%d]\n", addr, port)
	sys := &SysClient{}
	// create iotclient
	sys.iotclient, _ = cbase.NewSSIoTClient(addr, port, service_selfname)
	sys.iotclient.SetMsgHandle(sys)
	go sys.iotclient.Connect()

	return sys, nil
}

// MsgHandle interface
func (sc *SysClient) IoTDealMessage(topic, msg string) error {
	jklog.L().Debugln("topic ", topic, ", msg ", msg)
	return nil
}

func (sc *SysClient) IoTStatusReport(status string) {
	jklog.L().Infoln("iot status changed to status ", status)
}

func (sc *SysClient) IoTPub(topic, data string) {
	jklog.L().Debugln("topic ", topic, " , data ", data)
	err := sc.iotclient.Pub(topic, data)
	if err != nil {
		jklog.L().Errorln("error send topic")
	}
}

